<template>
  <el-container style="height:100%">
    <!-- 头部 -->
    <el-header>
      <div class="header-log">
        <div style="width:100px">
          <img src="https://usave.co.uk/img/logo-header.svg" alt="图片不见了" />
        </div>
        <div style="display：flex;margin-left:10px">
          <span>商城后台管理</span>
          
          
        </div>
        <div style="margin-left:5px" @click="toggle"><i class="el-icon-s-unfold"></i></div>
      </div>
      <div>
        <el-button @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#1a8cff"
          unique-opened=false
          :collapse='this.isCollapse'
          collapse-transition = false
          router="true"
         
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+ ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children " :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      menuList: [],
      iconsList:{
        '125': 'el-icon-user',
        '103': 'el-icon-star-off',
        '101': 'el-icon-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-office-building'
      },
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    };
  },
  mounted(){
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList(){
      const {data: res} =await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data
      console.log(this.menuList)
    },
    // 控制左侧单航栏样式
    toggle(){
      this.isCollapse = !this.isCollapse
    },
    // 保存连接转态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }

  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-header,
.el-footer {
  background-color: #06101d;

  line-height: 60px;
}

.el-aside {
  background-color: #5d646f;
  color: #333;
  text-align: center;
  line-height: 100%;
  height: 100%;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  
  
  height: 100%;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 100%;
}

.el-container:nth-child(7) .el-aside {
  line-height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  color: #e9eef3;
  font: 20px;
  margin-left: 0px;
}

.header-log {
  display: flex;
  width: 20%;
}

.el-menu{
  background-origin: none;
}
</style>
